package cn.dt70.controller;

import cn.dt70.pojo.AqbInvestment;
import cn.dt70.pojo.AqbPacket;
import cn.dt70.pojo.AqbReward;
import cn.dt70.pojo.AqbUser;
import cn.dt70.service.AqbPacketService;
import cn.dt70.service.AqbRewardService;
import cn.dt70.utils.Constants;
import cn.dt70.utils.PageSupport;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/reward/")
public class RewardController {
    private static final Logger logger = Logger.getLogger(RewardController.class);

    @Resource
    private AqbRewardService aqbRewardService;

    @RequestMapping("show")
    public String showreward(HttpSession session,
                             Model model
    ) throws Exception {
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        List<AqbReward> listReward = aqbRewardService.getRewards(aqbUser.getUserId());
        int couldUseReward = aqbRewardService.sumReward(aqbUser.getUserId(), 1);
        int canNotUseReward = aqbRewardService.sumReward(aqbUser.getUserId(), 0);
        int surplus = aqbRewardService.surplus(aqbUser.getUserId(), 1);
        model.addAttribute("surplus", surplus);
        model.addAttribute("listReward", listReward);
        model.addAttribute("couldUseReward", couldUseReward);
        model.addAttribute("canNotUseReward", canNotUseReward);
        return "usercenter-rewardcontrol-rewardrecord";

    }
    @RequestMapping(value = "useReward", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String useReward(HttpSession session
                           ) throws Exception {

        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        int surpluss = aqbRewardService.surplus(aqbUser.getUserId(), 1);
        StringBuffer sb = new StringBuffer("{");
        sb.append("\"surpluss\":" + JSON.toJSONString(surpluss) + "");
        sb.append("}");
        return sb.toString();
    }


    @RequestMapping(value = "rewardLists", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String showRewardlist(HttpSession session,
                                 @RequestParam("rewardType") String rewardType,
                                 @RequestParam("startDay") String startDay
    ) throws Exception {
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        if ("3".equals(rewardType)) {
            List<AqbReward> listReward = aqbRewardService.getAllRewardByStatus(aqbUser.getUserId(), null, Integer.valueOf(startDay));
            StringBuffer sb = new StringBuffer("{");
            sb.append("\"listRewardByZore\":");
            String listRewards = JSON.toJSONString(listReward);
            sb.append(listRewards);
            sb.append("}");
            return sb.toString();
        } else if ("2".equals(rewardType)) {
            List<AqbReward> listRewardByOne = aqbRewardService.getAllRewardByStatus(aqbUser.getUserId(), 1, Integer.valueOf(startDay));
            StringBuffer sb = new StringBuffer("{");
            sb.append("\"listRewardByZore\":");
            String listRewardByone = JSON.toJSONString(listRewardByOne);
            sb.append(listRewardByone);
            sb.append("}");
            return sb.toString();
        } else{
            List<AqbReward> listRewardByZore = aqbRewardService.getAllRewardByStatus(aqbUser.getUserId(), 0, Integer.valueOf(startDay));
            StringBuffer sb = new StringBuffer("{");
            sb.append("\"listRewardByZore\":");
            String listRewardByzore = JSON.toJSONString(listRewardByZore);
            sb.append(listRewardByzore);
            sb.append("}");
            return sb.toString();
        }
    }

    @RequestMapping(value = "rewardListss", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String showRewardlists(HttpSession session,
                                 @RequestParam("rewardType") String rewardType,
                                 @RequestParam("begin") String begin,
                                  @RequestParam("end") String end
    ) throws Exception {
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        if ("3".equals(rewardType)) {
            List<AqbReward> listReward = aqbRewardService.getAllRewardByStatusAndTime(aqbUser.getUserId(), null,simpleDateFormat.parse(begin),simpleDateFormat.parse(end));
            StringBuffer sb = new StringBuffer("{");
            sb.append("\"listRewardByZore\":");
            String listRewards = JSON.toJSONString(listReward);
            sb.append(listRewards);
            sb.append("}");
            return sb.toString();
        } else if ("2".equals(rewardType)) {
            List<AqbReward> listRewardByOne = aqbRewardService.getAllRewardByStatusAndTime(aqbUser.getUserId(), 1,simpleDateFormat.parse(begin),simpleDateFormat.parse(end));
            StringBuffer sb = new StringBuffer("{");
            sb.append("\"listRewardByZore\":");
            String listRewardByone = JSON.toJSONString(listRewardByOne);
            sb.append(listRewardByone);
            sb.append("}");
            return sb.toString();
        } else{
            List<AqbReward> listRewardByZore = aqbRewardService.getAllRewardByStatusAndTime(aqbUser.getUserId(), 0,simpleDateFormat.parse(begin),simpleDateFormat.parse(end));
            StringBuffer sb = new StringBuffer("{");
            sb.append("\"listRewardByZore\":");
            String listRewardByzore = JSON.toJSONString(listRewardByZore);
            sb.append(listRewardByzore);
            sb.append("}");
            return sb.toString();
        }
    }
}


//    @RequestMapping("rewardList")
//    public String showRewards(HttpSession session,
//                              @RequestParam(value = "time1",required = false)String time1,
//                              @RequestParam(value = "time2",required = false)String time2,
//                              @RequestParam(value = "days",required = false)String days,
//                              PageSupport ps,
//                              @RequestParam("currentPageNo") String currentPageNo,
//                              @RequestParam(value = "status",required = false)String status,
//                              @RequestParam(value = "start",required = false)String start
//                              ) throws Exception {
//        if (null == currentPageNo || "".equals(currentPageNo)) {
//            ps.setCurrentPageNo(0);
//        } else {
//            ps.setCurrentPageNo(Integer.parseInt(currentPageNo));
//        }
//
//        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
//
//        List<AqbReward> aqbRewardList = aqbRewardService.getAllReward(aqbUser.getUserId(),Integer.parseInt(status),Integer.parseInt(days),ps);
//
//
//        String reward = JSON.toJSONString(aqbRewardList);
//        String phs = JSON.toJSONString(ps);
//        StringBuffer sb = new StringBuffer("{");
//        sb.append("\"reward\":" + reward + "");
//        sb.append(",\"phs\":" + phs + "");
//        sb.append("}");
//        return sb.toString();
//    }
//    public String showPersonCount(@RequestParam(value = "status",required = false) String status,
//                                  @RequestParam(value = "days",required = false) String days,
//                                  @RequestParam(value = "begin",required = false) String begin,
//                                  @RequestParam(value = "end",required = false) String end,
//                                  PageSupport ps,
//                                  HttpSession session,
//                                  Model model,
//                                  @RequestParam(value = "currentPageNo",required = false) String currentPageNo) throws Exception {
//        if (null == currentPageNo || "".equals(currentPageNo)) {
//            ps.setCurrentPageNo(0);
//        } else {
//            ps.setCurrentPageNo(Integer.parseInt(currentPageNo));
//        }
//        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
//        List<AqbReward> aqbRewardList = aqbRewardService.getAllReward(aqbUser.getUserId(),Integer.valueOf(status) - 1,Integer.valueOf(days),ps);
//        model.addAttribute("aqbRewardList",aqbRewardList);
//
//        return "usercenter-rewardcontrol-rewardrecord";
//    }

//    @RequestMapping("investment")
//    public String displayInvestment(@RequestParam("id") String id,@RequestParam(value = "opr",required = false)String opr,@RequestParam(value = "begin",required = false)String begin,@RequestParam(value = "end",required = false)String end, Model model) throws Exception {
//        if(null!=opr) {
//            //一周内的投资
//            if (opr.equals("week")) {
//                List<AqbInvestment> InvestmentList = aqbInvestmentService.findByIdInWeek(Integer.parseInt(id));
//                model.addAttribute("InvestmentList", InvestmentList);
//                return "usercenter-investcontrol-investrecord";
//                //一月内的投资
//            } else if (opr.equals("month")) {
//                List<AqbInvestment> InvestmentList = aqbInvestmentService.findByIdInMonth(Integer.parseInt(id));
//                model.addAttribute("InvestmentList", InvestmentList);
//                return "usercenter-investcontrol-investrecord";
//                //三月内的投资
//            } else if (opr.equals("threeMonth")) {
//                List<AqbInvestment> InvestmentList = aqbInvestmentService.findByIdInThreeMonth(Integer.parseInt(id));
//                model.addAttribute("InvestmentList", InvestmentList);
//                return "usercenter-investcontrol-investrecord";
//            }
//        }
//
//        //一段时间内的投资
//        if(begin!=null&&end!=null){
//            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//
//            List<AqbInvestment> InvestmentList = aqbInvestmentService.findByIdForTime(Integer.parseInt(id), simpleDateFormat.parse(begin),simpleDateFormat.parse(end));
//            model.addAttribute("InvestmentList",InvestmentList);
//            return "usercenter-investcontrol-investrecord";
//        }
//
//        List<AqbInvestment> InvestmentList = aqbInvestmentService.findById(Integer.parseInt(id));
//        model.addAttribute("InvestmentList",InvestmentList);
//        return "usercenter-investcontrol-investrecord";
//    }
//
//    @RequestMapping("transfer")
//    public String displayTransfer(@RequestParam("id") String id,Model model) throws Exception {
//        List<AqbInvestment> InvestmentList = aqbInvestmentService.findCreditorById(Integer.parseInt(id));
//        model.addAttribute("InvestmentList",InvestmentList);
//        return "usercenter-investcontrol-transfer";
//    }

//    @RequestMapping("rewardList")
//    public String showRewardList(@RequestParam(value = "status",required = false) String status,
//                                 @RequestParam(value = "days",required = false) String days,
//                                 PageSupport ps,
//                                 HttpSession session,
//                                 Model model,
//                                 @RequestParam("currentPageNo") String currentPageNo) throws Exception {
//
//
//
//
//    }




